<?php

class Product extends MY_Controller {
    
    public function __construct()
    {
        parent::__construct();
        $this->load->library('pagination'); 
        $this->load->model("brand_home_model");
        $this->load->model("category_home_model");
       // $this->load->model("slider_home_model");
        $this->load->model("product_home_model");
        
    }   

    public function brand($id)
    {
        
        $data['brand_name'] = $this->brand_home_model->get_info('bra_id', $id);
        $start = $this->uri->segment(3);
        if(!$start) $start = 0; 
        $data['list_pro'] = $this->product_home_model->brand_pro($id, $start, 4);
        $config['base_url'] = base_url()."/index.php/".$id."/brand.html/"; // xác định trang phân trang 
        $config['total_rows'] = count($this->product_home_model->brand_pro($id)); // xác định tổng số record 
        $config['per_page'] = 4; // xác định số record ở mỗi trang 
        $config['uri_segment'] = 3; // xác định segment chứa page number 
        $this->pagination->initialize($config);
        $data['page_link'] = $this->pagination->create_links();
        $this->layout_home('product/index',$data);
    }
    public function category($id)
    {
       
        $data['cate_name'] = $this->category_home_model->get_info('cate_id', $id);
        $start = $this->uri->segment(3);
        if(!$start) $start = 0;
        $data['list_pro'] = $this->product_home_model->cate_pro($id, $start, 6);
        $config['base_url'] = base_url()."/index.php/".$id."/category.html/";
        $config['total_rows'] = count($this->product_home_model->cate_pro($id));
        $config['per_page'] = 6;
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['page_link'] = $this->pagination->create_links();
        
       $this->layout_home('product/index',$data);
    }
    public function sortcate()
    {
        $val = $this->input->post('sortcate');
        $id = $this->input->post('idcate');
        $data = $this->product_home_model->cate_pro($id,0,'',$val);
       
        $html = $this->help_product_helper->show_product($data);
        echo $html;
        
    }
    public function sortbrand()
    {
        $val = $this->input->post('sortbrand');
        $id = $this->input->post('idbrand');
        $data = $this->product_home_model->brand_pro($id,0,'',$val);
        $html = $this->help_product_helper->show_product($data);
        echo $html;
        
    }
    public function range_price()
    {
        $start = $this->input->post('start');
        $end = $this->input->post('end');
        $data = $this->product_home_model->list_range_price($start, $end);
        $html = $this->help_product_helper->show_product($data);
        echo $html;
    }
    public function details($id_pro)
    {
        $data['product'] = $this->product_home_model->get_info($id_pro);
        $data['imgs'] = $this->product_home_model->get_imgs($id_pro);
        $this->layout_home('product/details', $data);
    }
    public function buy($id_pro)
    {
        $info_pro = $this->product_home_model->get_info($id_pro);
        foreach ($info_pro as $pro)
        {
            $pro_name = $pro['pro_name'];
            $pro_price = $pro['pro_pri_sale'];
        }
        
        $datapr = array(
            "name"=>$pro_name,
            "sale"=>$pro_price);
        
        $this->session->set_userdata('cart',$datapr);
        $this->layout_home('product/buy');
    }
}

